﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using nRoute.Components;
using nRoute.Navigation;

namespace nRoute.Samples.FutureDesktop.Controls
{
    public partial class WorkspaceControl 
        : UserControl
    {
        private const string WORKSPACE_TITLE = "WORKSPACE";
        private const string ACTIVESTATE_NAME = "ActiveState";
        private const string COLLAPSEDSTATE_NAME = "CollapsedState";

        public static readonly DependencyProperty IsActiveProperty =
            DependencyProperty.Register("IsActive", typeof(bool), typeof(WorkspaceControl),
            new PropertyMetadata(false, new PropertyChangedCallback(OnValueChanged)));
        
        public event EventHandler ActivateWorkspace;

        public WorkspaceControl()
        {
            InitializeComponent();
            this.SetRelayConverter<string, string>("TitleConverter", (s) => (string.IsNullOrEmpty(s)) ? WORKSPACE_TITLE : s.ToUpper());
        }

#region Properties

        public bool IsActive
        {
            get { return Convert.ToBoolean(GetValue(IsActiveProperty)); }
            set { SetValue(IsActiveProperty, value);}
        }

#endregion

#region Event Handler

        private void Blade_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (ActivateWorkspace != null) ActivateWorkspace(this, EventArgs.Empty);
        }

#endregion

#region Handlers

        private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((WorkspaceControl)d).UpdateIsActive(Convert.ToBoolean(e.NewValue));
        }

        private void UpdateIsActive(bool value)
        {
            VisualStateManager.GoToState(this, value ? ACTIVESTATE_NAME : COLLAPSEDSTATE_NAME, true);
            NavigationService.DefaultNavigationHandler = this.WorkspaceContainer;       // set as the default app-wide handler
        }

#endregion

    }
}